{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PyGeM"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### Tutorial 6: Free Form Deformation on a k mesh on cylinder: continuity of the deformation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this tutorial we show how to perform the Free Form Deformation on a hexaedral mesh of a cylinder. As seen in Tutorial 3, but with a LS-Dyna k file.\n",
    "\n",
    "First of all we just import the required PyGeM classes and we read a parameters file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pygem import FFDParameters, FFD, KHandler, VtkHandler\n",
    "import vtkplotter as vtkp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "params = FFDParameters()\n",
    "params.read_parameters(filename='../tests/test_datasets/parameters_test_ffd_pipe_unv_C0.prm')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following is the parameters file for the case at hand. In particular, if you look at the Box info section, there is 2-by-2-by-3 lattice around a cylinder. Since we want to shift the middle section of the cylinder along the x direction we modify only the parameter x weights corresponding to the middle point in z direction (index==1). In the following we show only the important parts of the parameters file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\r\n",
      "[Box info]\r\n",
      "# This section collects all the properties of the FFD bounding box.\r\n",
      "\r\n",
      "# n control points indicates the number of control points in each direction (x, y, z).\r\n",
      "# For example, to create a 2 x 3 x 2 grid, use the following: n control points: 2, 3, 2\r\n",
      "n control points x: 2\r\n",
      "n control points y: 2\r\n",
      "n control points z: 3\r\n",
      "\r\n",
      "# box length indicates the length of the FFD bounding box along the three canonical directions (x, y, z).\r\n",
      "# It uses the local coordinate system.\r\n",
      "# For example to create a 2 x 1.5 x 3 meters box use the following: box length: 2.0, 1.5, 3.0\r\n",
      "box length x: 2.2\r\n",
      "box length y: 2.2\r\n",
      "box length z: 6.0\r\n",
      "\r\n",
      "# box origin indicates the x, y, and z coordinates of the origin of the FFD bounding box. That is center of\r\n",
      "# rotation of the bounding box. It corresponds to the point coordinates with position [0][0][0].\r\n",
      "# See section \"Parameters weights\" for more details.\r\n",
      "# For example, if the origin is equal to 0., 0., 0., use the following: box origin: 0., 0., 0.\r\n",
      "box origin x: -1.1\r\n",
      "box origin y: -1.1\r\n",
      "box origin z: 2.0\r\n",
      "\r\n",
      "# rotation angle indicates the rotation angle around the x, y, and z axis of the FFD bounding box in degrees.\r\n",
      "# The rotation is done with respect to the box origin.\r\n",
      "# For example, to rotate the box by 2 deg along the z direction, use the following: rotation angle: 0., 0., 2.\r\n",
      "rotation angle x: 0.0\r\n",
      "rotation angle y: 0.0\r\n",
      "rotation angle z: 0.0\r\n",
      "\r\n",
      "\r\n",
      "[Parameters weights]\r\n",
      "# This section describes the weights of the FFD control points.\r\n",
      "# We adopt the following convention:\r\n",
      "# For example with a 2x2x2 grid of control points we have to fill a 2x2x2 matrix of weights.\r\n",
      "# If a weight is equal to zero you can discard the line since the default is zero.\r\n",
      "#\r\n",
      "# | x index | y index | z index | weight |\r\n",
      "#  --------------------------------------\r\n",
      "# |    0    |    0    |    0    |  1.0   |\r\n",
      "# |    0    |    1    |    1    |  0.0   | --> you can erase this line without effects\r\n",
      "# |    0    |    1    |    0    | -2.1   |\r\n",
      "# |    0    |    0    |    1    |  3.4   |\r\n",
      "\r\n",
      "# parameter x collects the displacements along x, normalized with the box length x.\r\n",
      "parameter x: 0   0   0   0.0\r\n",
      "             0   0   1   0.8\r\n",
      "             0   0   2   0.0\r\n",
      "             0   1   0   0.0\r\n",
      "             0   1   1   0.8\r\n",
      "             0   1   2   0.0\r\n",
      "             1   0   0   0.0\r\n",
      "             1   0   1   0.8\r\n",
      "             1   0   2   0.0\r\n",
      "             1   1   0   0.0\r\n",
      "             1   1   1   0.8\r\n",
      "             1   1   2   0.0\r\n",
      "\r\n",
      "# parameter y collects the displacements along y, normalized with the box length y.\r\n",
      "parameter y: 0   0   0   0.0\r\n",
      "             0   0   1   0.0\r\n",
      "             0   0   2   0.0\r\n",
      "             0   1   0   0.0\r\n",
      "             0   1   1   0.0\r\n",
      "             0   1   2   0.0\r\n",
      "             1   0   0   0.0\r\n",
      "             1   0   1   0.0\r\n",
      "             1   0   2   0.0\r\n",
      "             1   1   0   0.0\r\n",
      "             1   1   1   0.0\r\n",
      "             1   1   2   0.0\r\n",
      "\r\n",
      "# parameter z collects the displacements along z, normalized with the box length z.\r\n",
      "parameter z: 0   0   0   0.0\r\n",
      "             0   0   1   0.0\r\n",
      "             0   0   2   0.0\r\n",
      "             0   1   0   0.0\r\n",
      "             0   1   1   0.0\r\n",
      "             0   1   2   0.0\r\n",
      "             1   0   0   0.0\r\n",
      "             1   0   1   0.0\r\n",
      "             1   0   2   0.0\r\n",
      "             1   1   0   0.0\r\n",
      "             1   1   1   0.0\r\n",
      "             1   1   2   0.0\r\n"
     ]
    }
   ],
   "source": [
    "%cat ../tests/test_datasets/parameters_test_ffd_pipe_unv_C0.prm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "We now load the k file!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "k_handler = KHandler()\n",
    "mesh_points = k_handler.parse('../tests/test_datasets/test_pipe.k')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and visualize the undeformed mesh.\n",
    "\n",
    "![](pictures/cylinder_orig_unv.png)\n",
    "\n",
    "We now, as always, perform the FFD and write out the results in a new k file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "free_form = FFD(params, mesh_points)\n",
    "free_form.perform()\n",
    "new_mesh_points = free_form.modified_mesh_points\n",
    "k_handler.write(new_mesh_points, 'test_pipe_mod_C0.k')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let us see the result.\n",
    "\n",
    "![](pictures/cylinder_mod_unv_C0.png)\n",
    "\n",
    "As you can easily see the deformation makes the mesh only C0 continuous.\n",
    "\n",
    "This is not wrong a priori, but it can have some drawbacks.\n",
    "\n",
    "First of all, if you start with a smooth geometry, probably you do not want to end up with a geometry with more edges.\n",
    "\n",
    "For large deformations this can cause inaccurancy errors when you try to perform analysis on the deformed mesh. In fact, it can happen that we have very stretched cells that the solver can not treat properly. Of course it depends mainly on the solver choosen, but, in general, it is not very clever to have \"spiky\" cells.\n",
    "\n",
    "Moreover, if you are interested in some quantities, such as grandients and normal vectors, it can be difficult to compute them close to the \"artificial edges\".\n",
    "\n",
    "Thus, we can rely on the properties of the Bernstein polynomials (on which FFD is built upon) to overcome this potential problem. In fact, if we add 2 new control points in the z direction we can move only the middle one, leaving the first two and the last two still. The new parameter file becomes:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%cat ../tests/test_datasets/parameters_test_ffd_pipe_unv_C1.prm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We again load the new parameter file, perform the FFD and write out the results with a new k file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "params = FFDParameters()\n",
    "params.read_parameters(filename='../tests/test_datasets/parameters_test_ffd_pipe_unv_C1.prm')\n",
    "\n",
    "free_form = FFD(params, mesh_points)\n",
    "free_form.perform()\n",
    "new_mesh_points = free_form.modified_mesh_points\n",
    "\n",
    "k_handler.write(new_mesh_points, 'test_pipe_mod_C1.k')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And here it is the C1 mesh on the cylinder.\n",
    "\n",
    "![](pictures/cylinder_mod_unv_C1.png)\n",
    "\n",
    "You can add some other points to increase again the continuity of the mesh!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
